(function() {

this.codium = this.codium || {};
this.codium.panel = this.codium.panel || {};

this.codium.panel.TicketItem = pasta.webgui.Panel.extend(
{
	init : function() 
	{
		this._super();
		
		this.canvas.style.background = "rgba(0, 0, 0, 0.4)";
		this.canvas.style.zIndex = "1000";
		
		this.service = new codium.CodiumService();
		
		this.pnl = new pasta.webgui.ContainerControl();
		this.pnl.setWidth(700);
		this.pnl.setHeight(400);
		this.pnl.setLeft((this.getWidth() - this.pnl.getWidth()) / 2);
		this.pnl.setTop((this.getHeight() - this.pnl.getHeight()) / 3);
		this.pnl.canvas.style.background = "rgba(255, 255, 255, 0.9)";
		this.pnl.canvas.style.border = "1px solid rgba(255, 255, 255, 1)";
		this.pnl.canvas.style.webkitBoxShadow = "0px 1px 8px #000000";
		this.pnl.canvas.style.mozBoxShadow = "0px 1px 8px #000000";
		this.pnl.canvas.style.boxShadow = "0px 1px 8px #000000";
		this.pnl.canvas.style.mozBorderRadius = "6px";
		this.pnl.canvas.style.webkitBorderRadius = "6px";
		this.pnl.canvas.style.borderRadius = "6px";
		this.addControl(this.pnl);
		
			this.lblTitle = new pasta.webgui.Label();
			this.lblTitle.setCaption(".: New Ticket");
			this.lblTitle.setWidth(this.pnl.getWidth());
			this.lblTitle.setTop(10);
			this.lblTitle.setLeft(10);
			this.lblTitle.font.setSize(16);
			this.lblTitle.font.setBold(true);
			this.pnl.addControl(this.lblTitle);
			
			this.btnClose = new pasta.webgui.ImageButton();
			this.btnClose.setLeft(this.pnl.getWidth() - 15);
			this.btnClose.setTop(-15);
			this.btnClose.setWidth(30);
			this.btnClose.setHeight(30);
			this.btnClose.setImage(this.libDir + "/img/button/closeBox.png");
			this.btnClose.getOnClick().setHandler(this, this.btnCloseClick);
			this.pnl.addControl(this.btnClose);
			
			this.pageControl = new codium.control.PageControl();
			this.pageControl.setLeft(10);
			this.pageControl.setTop(45);
			this.pageControl.setWidth(this.pnl.getWidth() - (2 * this.pageControl.getLeft()));
			this.pageControl.setHeight(this.pnl.getHeight() - this.pageControl.getTop() - this.pageControl.getLeft());
			this.pnl.addControl(this.pageControl);
			
			this.pageInfo = new codium.control.Page();
			this.pageInfo.setCaption("Info");
			this.pageControl.addControl(this.pageInfo);
			
				this.lblReporterTitle = new pasta.webgui.Label();
				this.lblReporterTitle.setLeft(10);
				this.lblReporterTitle.setTop(10);
				this.lblReporterTitle.setWidth(100);
				this.lblReporterTitle.font.setBold(true);
				this.lblReporterTitle.font.setColor("#7F0000");
				this.lblReporterTitle.setCaption("Reporter :");
				this.pageInfo.addControl(this.lblReporterTitle);
				
				this.lblReporter = new pasta.webgui.Label();
				this.lblReporter.setLeft(this.lblReporterTitle.getLeft() + this.lblReporterTitle.getWidth());
				this.lblReporter.setTop(this.lblReporterTitle.getTop());
				this.lblReporter.setWidth(200);
				this.lblReporter.setCaption("");
				this.pageInfo.addControl(this.lblReporter);
				
				this.lblReportDateTitle = new pasta.webgui.Label();
				this.lblReportDateTitle.setLeft(10);
				this.lblReportDateTitle.setTop(35);
				this.lblReportDateTitle.setWidth(100);
				this.lblReportDateTitle.font.setBold(true);
				this.lblReportDateTitle.font.setColor("#7F0000");
				this.lblReportDateTitle.setCaption("Report Date :");
				this.pageInfo.addControl(this.lblReportDateTitle);
				
				this.lblReportDate = new pasta.webgui.Label();
				this.lblReportDate.setLeft(this.lblReportDateTitle.getLeft() + this.lblReportDateTitle.getWidth());
				this.lblReportDate.setTop(this.lblReportDateTitle.getTop());
				this.lblReportDate.setWidth(200);
				this.lblReportDate.setCaption("");
				this.pageInfo.addControl(this.lblReportDate);
				
				this.lblAsigneeTitle = new pasta.webgui.Label();
				this.lblAsigneeTitle.setLeft(this.pageInfo.getWidth() / 2);
				this.lblAsigneeTitle.setTop(10);
				this.lblAsigneeTitle.setWidth(100);
				this.lblAsigneeTitle.font.setBold(true);
				this.lblAsigneeTitle.font.setColor("#7F0000");
				this.lblAsigneeTitle.setCaption("Assigned To :");
				this.pageInfo.addControl(this.lblAsigneeTitle);
				
				this.lblAsignee = new pasta.webgui.Label();
				this.lblAsignee.setLeft(this.lblAsigneeTitle.getLeft() + this.lblAsigneeTitle.getWidth());
				this.lblAsignee.setTop(this.lblAsigneeTitle.getTop());
				this.lblAsignee.setWidth(200);
				this.lblAsignee.setCaption("");
				this.pageInfo.addControl(this.lblAsignee);
				
				this.lblDueDateTitle = new pasta.webgui.Label();
				this.lblDueDateTitle.setLeft(this.pageInfo.getWidth() / 2);
				this.lblDueDateTitle.setTop(35);
				this.lblDueDateTitle.setWidth(100);
				this.lblDueDateTitle.font.setBold(true);
				this.lblDueDateTitle.font.setColor("#7F0000");
				this.lblDueDateTitle.setCaption("Due Date :");
				this.pageInfo.addControl(this.lblDueDateTitle);
				
				this.lblDueDate = new pasta.webgui.Label();
				this.lblDueDate.setLeft(this.lblDueDateTitle.getLeft() + this.lblDueDateTitle.getWidth());
				this.lblDueDate.setTop(this.lblDueDateTitle.getTop());
				this.lblDueDate.setWidth(200);
				this.lblDueDate.setCaption("");
				this.pageInfo.addControl(this.lblDueDate);
				
				this.lblStateTitle = new pasta.webgui.Label();
				this.lblStateTitle.setLeft(this.pageInfo.getWidth() / 2);
				this.lblStateTitle.setTop(60);
				this.lblStateTitle.setWidth(100);
				this.lblStateTitle.font.setBold(true);
				this.lblStateTitle.font.setColor("#7F0000");
				this.lblStateTitle.setCaption("State :");
				this.pageInfo.addControl(this.lblStateTitle);
				
				this.lblState = new pasta.webgui.Label();
				this.lblState.setLeft(this.lblStateTitle.getLeft() + this.lblStateTitle.getWidth());
				this.lblState.setTop(this.lblStateTitle.getTop());
				this.lblState.setWidth(200);
				this.lblState.setCaption("");
				this.pageInfo.addControl(this.lblState);
				
				this.mmoDesc = new pasta.webgui.Memo();
				this.mmoDesc.setLeft(10);
				this.mmoDesc.setTop(90);
				this.mmoDesc.setWidth(this.pageInfo.getWidth() - (2 * this.mmoDesc.getLeft()));
				this.mmoDesc.setHeight(this.pageInfo.getHeight() - this.mmoDesc.getTop() - 50);
				this.mmoDesc.setReadOnly(true);
				this.pageInfo.addControl(this.mmoDesc);
				
				this.btnEdit = new pasta.webgui.Button();
				this.btnEdit.setLeft(this.mmoDesc.getLeft());
				this.btnEdit.setTop(this.mmoDesc.getTop() + this.mmoDesc.getHeight() + 10);
				this.btnEdit.setCaption("Edit");
				this.btnEdit.setWidth(80);
				this.btnEdit.getOnClick().setHandler(this, this.btnEditClick);
				this.pageInfo.addControl(this.btnEdit);
				
				this.btnOp1 = new pasta.webgui.Button();
				this.btnOp1.setLeft(this.pageInfo.getWidth() - 100);
				this.btnOp1.setTop(this.btnEdit.getTop());
				this.btnOp1.setCaption("OP 1");
				this.btnOp1.setWidth(90);
				this.btnOp1.getOnClick().setHandler(this, this.btnOp1Click);
				this.pageInfo.addControl(this.btnOp1);
				
				this.btnOp2 = new pasta.webgui.Button();
				this.btnOp2.setLeft(this.btnOp1.getLeft() - 93);
				this.btnOp2.setTop(this.btnEdit.getTop());
				this.btnOp2.setCaption("OP 2");
				this.btnOp2.setWidth(90);
				this.btnOp2.getOnClick().setHandler(this, this.btnOp2Click);
				this.pageInfo.addControl(this.btnOp2);
				
			this.pageAttachment = new codium.control.Page();
			this.pageAttachment.setCaption("Attachment");
			this.pageControl.addControl(this.pageAttachment);
			
				this.listFile = new codium.control.AttachmentList();
				this.listFile.setLeft(10);
				this.listFile.setTop(10);
				this.listFile.setWidth(this.pageAttachment.getWidth() - (2 * this.listFile.getLeft()));
				this.listFile.setHeight(this.pageAttachment.getHeight() - this.listFile.getTop() - 50);
				this.pageAttachment.addControl(this.listFile);
				
				this.btnNewFile = new pasta.webgui.Button();
				this.btnNewFile.setLeft(this.listFile.getLeft());
				this.btnNewFile.setTop(this.listFile.getTop() + this.listFile.getHeight() + 5);
				this.btnNewFile.setCaption("New Attachment");
				this.btnNewFile.setWidth(115);
				this.btnNewFile.getOnClick().setHandler(this, this.btnNewFileClick);
				this.pageAttachment.addControl(this.btnNewFile);
			
			this.pageComment = new codium.control.Page();
			this.pageComment.setCaption("Comment");
			this.pageControl.addControl(this.pageComment);
			
			this.pageSolution = new codium.control.Page();
			this.pageSolution.setCaption("Solution");
			this.pageControl.addControl(this.pageSolution);
			
				this.mmoSolution = new pasta.webgui.Memo();
				this.mmoSolution.setLeft(10);
				this.mmoSolution.setTop(10);
				this.mmoSolution.setWidth(this.pageSolution.getWidth() - (2 * this.mmoSolution.getLeft()));
				this.mmoSolution.setHeight(this.pageSolution.getHeight() - (2 * this.mmoSolution.getTop()));
				this.mmoSolution.setReadOnly(true);
				this.pageSolution.addControl(this.mmoSolution);
			
			this.pageRejection = new codium.control.Page();
			this.pageRejection.setCaption("Rejection");
			this.pageControl.addControl(this.pageRejection);
			
				this.mmoRejection = new pasta.webgui.Memo();
				this.mmoRejection.setLeft(10);
				this.mmoRejection.setTop(10);
				this.mmoRejection.setWidth(this.pageRejection.getWidth() - (2 * this.mmoRejection.getLeft()));
				this.mmoRejection.setHeight(this.pageRejection.getHeight() - (2 * this.mmoRejection.getTop()));
				this.mmoRejection.setReadOnly(true);
				this.pageRejection.addControl(this.mmoRejection);
			
		this.setVisible(false);
	},
	
	show : function(obj)
	{
		this.obj = obj;
		
		this.initSize();
		
		this._super();
		
		this.lblTitle.setCaption(".: Ticket No. " + obj.no + " - " + obj.ticketTitle);
		
		this.lblReporter.setCaption(obj.createUserName);
		this.lblReportDate.setCaption(obj.createDate);
		this.lblAsignee.setCaption(obj.assigneeUserName);
		this.lblDueDate.setCaption(obj.dueDate);
		this.mmoDesc.setText(obj.description);
		this.mmoSolution.setText(obj.resolution);
		this.mmoRejection.setText(obj.rejectReason);
		
		var statusString = "Created";
		var status = parseInt(obj.state);
		
		switch (status)
		{
			case 1 :
					statusString = "Created";
					this.btnOp1.setVisible(true);
					this.btnOp1.setCaption("Accept");
					this.btnOp2.setVisible(true);
					this.btnOp2.setCaption("Reject");
					break;
			case 2 :
					statusString = "Accepted";
					this.btnOp1.setVisible(true);
					this.btnOp1.setCaption("In Progress");
					this.btnOp2.setVisible(false);
					break;
			case 3 :
					statusString = "In Progress";
					this.btnOp1.setVisible(true);
					this.btnOp1.setCaption("Complete");
					this.btnOp2.setVisible(false);
					break;
			case 4 :
					statusString = "Rejected";
					this.btnOp1.setVisible(true);
					this.btnOp1.setCaption("Close");
					this.btnOp2.setVisible(false);
					break;
			case 5 :
					statusString = "Completed";
					this.btnOp1.setVisible(true);
					this.btnOp1.setCaption("Close");
					this.btnOp2.setVisible(false);
					break;
			case 6 :
					statusString = "Closed";
					this.btnOp1.setVisible(false);
					this.btnOp2.setVisible(false);
					break;
			default :
					statusString = "Undefined";
					this.btnOp1.setVisible(false);
					this.btnOp2.setVisible(false);
					break;
		}
		
		if (obj.assigneeUserId != app.userObj.userId)
		{
			this.btnOp1.setVisible(false);
			this.btnOp2.setVisible(false);
		}
		
		if (obj.createUserId == app.userObj.userId)
		{
			this.btnEdit.setVisible(true);
		}
		else
		{
			this.btnEdit.setVisible(false);
		}
		
		this.lblState.setCaption(statusString);
		
		this.pageControl.setActivePage(this.pageInfo);
		
		this.refreshFile();
	},
	
	initSize : function()
	{
		this.pnl.setLeft((this.getWidth() - this.pnl.getWidth()) / 2);
		this.pnl.setTop((this.getHeight() - this.pnl.getHeight()) / 3);
	},
	
	refreshFile : function()
	{
		var target = this;
		this.service.listTicketAttachment(this.obj.id, 
											function(result)
											{
												target.updateList(result);
											});
	},
	
	updateList : function(list)
	{
		this.listFile.clear();
		
		for (var i = 0; i < list.length; i++)
		{
			var obj = list[i];
			
			this.listFile.addFile(obj.id, obj.fileName, obj.userName, obj.uploadTime, obj.description);
		}
	},
	
	//--------------------- Event ---------------------
	
	btnNewFileClick : function(sender)
	{
		if (this.pnlNewFile == null)
		{
			this.pnlNewFile = new codium.panel.NewFile();
			this.pnlNewFile.getOnOK().setHandler(this, this.pnlNewFileOK);
			this.pnlNewFile.setWidth(stage.getWidth());
			this.pnlNewFile.setHeight(stage.getHeight());
			stage.addControl(this.pnlNewFile);
		}
		
		this.pnlNewFile.show("attachment", this.obj.id);
	},
	
	pnlNewFileOK : function(sender)
	{
		this.refreshFile();
	},
	
	btnOp1Click : function(sender)
	{
		switch (parseInt(this.obj.state))
		{
			case 1 : // create -> accept
					if (this._onChangeState != null)
						this._onChangeState.callHandler(this, this.obj.id, 2);
					break;
			case 2 : // accept -> in progress
					if (this._onChangeState != null)
						this._onChangeState.callHandler(this, this.obj.id, 3);
					break;
			case 3 : // in progress -> complete
					if (this.pnlSolution == null)
					{
						this.pnlSolution = new codium.panel.Solution();
						this.pnlSolution.setWidth(stage.getWidth());
						this.pnlSolution.setHeight(stage.getHeight());
						this.pnlSolution.getOnOK().setHandler(this, this.pnlSolutionOK);
						stage.addControl(this.pnlSolution);
					}
					
					this.pnlSolution.show("Solution");
					break;
			case 4 : // rejected -> closed
			case 5 : // complete -> closed
					if (this._onChangeState != null)
						this._onChangeState.callHandler(this, this.obj.id, 6);
					break;
		}
	},
	
	btnOp2Click : function(sender)
	{
		switch (parseInt(this.obj.state))
		{
			case 1 : // create -> rejected
					if (this.pnlSolution == null)
					{
						this.pnlSolution = new codium.panel.Solution();
						this.pnlSolution.setWidth(stage.getWidth());
						this.pnlSolution.setHeight(stage.getHeight());
						this.pnlSolution.getOnOK().setHandler(this, this.pnlSolutionOK);
						stage.addControl(this.pnlSolution);
					}
					
					this.pnlSolution.show("Rejection");
					break;
		}
	},
	
	pnlSolutionOK : function(sender, msg)
	{
		switch (parseInt(this.obj.state))
		{
			case 1 : // create -> rejected
					if (this._onChangeState != null)
						this._onChangeState.callHandler(this, this.obj.id, 4, msg);
					break;
			case 3 : // in progress -> complete
					if (this._onChangeState != null)
						this._onChangeState.callHandler(this, this.obj.id, 5, msg);
					break;
		}
	},
	
	btnCloseClick : function(sender)
	{
		this.close();
	},
	
	//--------------------- Setter & Getter ---------------------
	
	setWidth : function(data)
	{
		this._super(data);
		this.initSize();
	},
	
	setHeight : function(data)
	{
		this._super(data);
		this.initSize();
	},
	
	getOnEdit : function()
	{
		if (this._onEdit == null)
			this._onEdit = new pasta.util.EventObj();
		
		return this._onEdit;
	},
	
	getOnChangeState : function()
	{
		if (this._onChangeState == null)
			this._onChangeState = new pasta.util.EventObj();
		
		return this._onChangeState;
	},
});
	
})();